/*
 * NewPatientDialog.java
 *
 * Created on May 25, 2008, 2:30 AM
 */
package org.potomak.patientmanager.gui;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.border.Border;

import org.potomak.patientmanager.persistence.JPoxJDO;
import org.potomak.patientmanager.tools.Validation;

/**
 *
 * @author  mobile
 */
public class NewPatientDialog extends javax.swing.JDialog {

    private JPoxJDO jpoxJDO;
    private Border defaultBorder;
    
    /** Creates new form NewPatientDialog */
    public NewPatientDialog(JFrame parent, JPoxJDO jpoxJDO) {
        super(parent);
        
        this.jpoxJDO = jpoxJDO;

        setResizable(false);

        parent.setEnabled(false);
        parent.setFocusableWindowState(false);

        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        initComponents();
        
        defaultBorder = birthDayField.getBorder();

        getRootPane().setDefaultButton(addPatientButton);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        patientInfoTab = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        heightLabel = new javax.swing.JLabel();
        birthDayLabel = new javax.swing.JLabel();
        genderLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        genderField = new javax.swing.JComboBox();
        nameField = new javax.swing.JTextField();
        birthDayField = new javax.swing.JTextField();
        heightField = new javax.swing.JTextField();
        addPatientButton = new javax.swing.JButton();
        cancelPatientButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New contact");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Contact info"));

        heightLabel.setText("Height (m)");

        birthDayLabel.setText("Date of birth");

        genderLabel.setText("Gender");

        nameLabel.setText("Name");

        genderField.setModel(new GenderComboBoxModel());

        nameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nameFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                nameFieldFocusLost(evt);
            }
        });
        nameField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameFieldKeyReleased(evt);
            }
        });

        birthDayField.setText("gg/mm/aaaa");
        birthDayField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                birthDayFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                birthDayFieldFocusLost(evt);
            }
        });
        birthDayField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                birthDayFieldKeyReleased(evt);
            }
        });

        heightField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                heightFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                heightFieldFocusLost(evt);
            }
        });
        heightField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                heightFieldKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(genderLabel)
                    .add(nameLabel)
                    .add(birthDayLabel)
                    .add(heightLabel))
                .add(99, 99, 99)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(nameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .add(genderField, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(heightField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .add(birthDayField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {birthDayField, heightField}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(genderLabel)
                    .add(genderField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(birthDayLabel)
                    .add(birthDayField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(heightLabel)
                    .add(heightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addPatientButton.setText("Add");
        addPatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPatientButtonActionPerformed(evt);
            }
        });

        cancelPatientButton.setText("Cancel");
        cancelPatientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelPatientButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout patientInfoTabLayout = new org.jdesktop.layout.GroupLayout(patientInfoTab);
        patientInfoTab.setLayout(patientInfoTabLayout);
        patientInfoTabLayout.setHorizontalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(patientInfoTabLayout.createSequentialGroup()
                        .add(cancelPatientButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addPatientButton))
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        patientInfoTabLayout.setVerticalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addPatientButton)
                    .add(cancelPatientButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {
        this.getParent().setEnabled(true);
        ((JFrame) this.getParent()).setFocusableWindowState(true);
        setVisible(false);
    }

private void addPatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPatientButtonActionPerformed

    if(!Validation.validateName(nameField.getText())) {
        return;
    }
    
    if(!Validation.validateHeight(heightField.getText())) {
        return;
    }
    
    if(!Validation.validateDate(birthDayField.getText())) {
        return;
    }
    
    String name = nameField.getText().trim();
    Date birthDay = null;
    try {
        birthDay = (new SimpleDateFormat("dd/MM/yyyy")).parse(birthDayField.getText());
    } catch (Exception e) {
        e.printStackTrace();
    }
    String gender = ((ListItem) genderField.getModel().getSelectedItem()).getKey().toString().trim();
    Double height = new Double(heightField.getText());
    
    boolean success = jpoxJDO.insertPatient(name, birthDay, gender, height, new Date());

    if (!success) {
        JOptionPane.showMessageDialog(this, "Contact info not saved.", "Error", JOptionPane.ERROR_MESSAGE);
    }

    ((Contacts) this.getParent()).updatePatientListData();
    this.getParent().setEnabled(true);
    ((JFrame) this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_addPatientButtonActionPerformed

private void cancelPatientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelPatientButtonActionPerformed
    this.getParent().setEnabled(true);
    ((JFrame) this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_cancelPatientButtonActionPerformed

private void validateName() {
    if(Validation.validateName(nameField.getText())) {
        nameField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.green, 2));
    }
    else {
        nameField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2));
    }
}

private void nameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameFieldFocusGained
    validateName();
}//GEN-LAST:event_nameFieldFocusGained

private void nameFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameFieldFocusLost
    if(Validation.validateName(nameField.getText())) {
        nameField.setBorder(defaultBorder);
    }
}//GEN-LAST:event_nameFieldFocusLost

private void nameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameFieldKeyReleased
    validateName();
}//GEN-LAST:event_nameFieldKeyReleased

private void validateBirthDay() {
    if(Validation.validateDate(birthDayField.getText())) {
        birthDayField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.green, 2));
    }
    else {
        birthDayField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2));
    }
}

private void birthDayFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_birthDayFieldFocusGained
    validateBirthDay();
}//GEN-LAST:event_birthDayFieldFocusGained

private void birthDayFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_birthDayFieldFocusLost
    if(Validation.validateDate(birthDayField.getText())) {
        birthDayField.setBorder(defaultBorder);
    }
}//GEN-LAST:event_birthDayFieldFocusLost

private void birthDayFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_birthDayFieldKeyReleased
    validateBirthDay();
}//GEN-LAST:event_birthDayFieldKeyReleased

private void validateHeight() {
    if(Validation.validateHeight(heightField.getText())) {
        heightField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.green, 2));
    }
    else {
        heightField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2));
    }
}

private void heightFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_heightFieldFocusGained
    validateHeight();
}//GEN-LAST:event_heightFieldFocusGained

private void heightFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_heightFieldFocusLost
    if(Validation.validateHeight(heightField.getText())) {
        heightField.setBorder(defaultBorder);
    }
}//GEN-LAST:event_heightFieldFocusLost

private void heightFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_heightFieldKeyReleased
    validateHeight();
}//GEN-LAST:event_heightFieldKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPatientButton;
    private javax.swing.JTextField birthDayField;
    private javax.swing.JLabel birthDayLabel;
    private javax.swing.JButton cancelPatientButton;
    private javax.swing.JComboBox genderField;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextField heightField;
    private javax.swing.JLabel heightLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPanel patientInfoTab;
    // End of variables declaration//GEN-END:variables
}
